/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jmodcog2.fwd.convolve;

import jmodcog2.fwd.SupervisedNet;

/**
 *
 * @author Mitch
 */
public class UntrainedConvolveNet2D extends SupervisedNet {

    @Constant
    protected final int[] layer_len_x;
    @Constant
    protected final int[] layer_len_y;
    @Constant
    protected final int[] convolve_len_x;
    @Constant
    protected final int[] convolve_len_y;
    @Constant
    protected final int[] step_len_x;
    @Constant
    protected final int[] step_len_y;


    public UntrainedConvolveNet2D(Convolve2DConfig config) {
        super(config);
        this.layer_len_x = config.createLayerLenX();
        this.layer_len_y = config.createConvolveLenY();
        this.convolve_len_x = config.createConvolveLenX();
        this.convolve_len_y = config.createConvolveLenY();
        this.step_len_x = config.createStepLenX();
        this.step_len_y = config.createStepLenY();
    }
    
    

    @Override
    protected int _conn_index(int layer_i, int node_i, int node_j) {
        int ix = 0;
        int iy = 0;
        int jx = 0;
        int jy = 0;
        int step_x = 0;
        int step_y = 0;
        //
        ix = _get2D_i(node_i, layer_len_y[layer_i]);
        iy = _get2D_j(node_i, layer_len_y[layer_i]);
        jx = _get2D_i(node_j, layer_len_y[layer_i + 1]);
        jy = _get2D_j(node_j, layer_len_y[layer_i + 1]);
        step_x = step_len_x[layer_i];
        step_y = step_len_y[layer_i];
        //
        int x = ix - jx * step_x;
        int y = iy - jy * step_y;

        if (x >= 0 && x < step_x && y >= 0 && y < step_y) {
            int len_y = convolve_len_y[layer_i];
            if (shared_conn) {
                return conns_index0[layer_i] + _get2D_n(x, y, len_y);
            } else {
                return conns_index0[layer_i]
                        + node_j * convolve_len_x[layer_i] * len_y
                        + _get2D_n(x, y, convolve_len_y[layer_i]);
            }
        }
        return -1;
    }
}
